package controller

import (
	"devops/code"
	"devops/controller/validation"
	"devops/middleware"
	"devops/models"
	"devops/service"
	"fmt"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

// @Summary 项目列表
// @Tags 项目
// @Produce  json
// @Success 200 {object} data.SuccessGetAllProject  	"成功返回"
// @Router /api/project/all [get]
func GetAllProject(c *gin.Context) {
	devops := service.NewDevOps()
	SuccessResponse(c, devops.GetAllProject())
}

// @Summary 工程概览
// @Tags 项目
// @Produce  json
// @Success 200 {object} data.SuccessGetProjectDashboard  	"成功返回"
// @Router /api/project/dashboard [get]
func GetProjectDashboard(c *gin.Context) {
	devops := service.NewDevOps()
	SuccessResponse(c, devops.GetProjectDashboard())
}

// @Summary 记录概览
// @Tags 项目
// @Produce  json
// @Param	query  query  validation.GetBuildDashboard  true  "传入参数"
// @Success 200 {object} data.SuccessGetBuildDashboard  	"成功返回"
// @Router /api/project/build/dashboard [get]
func GetBuildDashboard(c *gin.Context) {
	var param validation.GetBuildDashboard
	if err := c.ShouldBind(&param); err != nil {
		ErrorResponse(c, code.PARAMETER_ERR, err.Error())
		return
	}
	validation.CheckPage(&param.Page)
	devops := service.NewDevOps()
	dashboard, total := devops.GetBuildDashboard(param.StartEnd.Start, param.StartEnd.End, param.Page.Page, param.Page.PageSize)
	SuccessPaginateResponse(c, dashboard, total, param.Page.Page, param.Page.PageSize)
}

// @Summary 创建项目
// @Tags 项目
// @Produce  json
// @Param	body  body  validation.CreateProject  true  "创建参数"
// @Success 200 {object} data.SuccessCreateProject  	"成功返回"
// @Failure 10000 {object} data.ParamError     			"错误返回"
// @Failure 11000 {object} data.FailCreateProject   	"错误返回"
// @Router /api/project/create [post]
func CreateProject(c *gin.Context) {
	var param validation.CreateProject
	if err := c.ShouldBind(&param); err != nil {
		ErrorResponse(c, code.PARAMETER_ERR, err.Error())
		return
	}
	param.Name = strings.TrimSpace(param.Name)
	devops := service.NewDevOps()
	user := c.MustGet("curUser").(middleware.User)
	uid, _ := strconv.Atoi(user.UserID)
	err := devops.CreateProject(param.Name, uid, param.Description, param.Icon)
	if err != nil {
		ErrorResponse(c, code.CREATE_FAIL, err.Error())
		return
	}
	// 记录事件
	go devops.ProjectEvent(service.ProjectCreate, &user, models.GetProjectByName(param.Name))
	SuccessResponse(c, fmt.Sprintf("%s 创建成功", param.Name))
}

// @Summary 删除项目
// @Tags 项目
// @Produce  json
// @Param	body  body  validation.DeleteProject  true  "删除参数"
// @Success 200 {object} data.SuccessDeleteProject      "成功返回"
// @Failure 10000 {object} data.ParamError     			"错误返回"
// @Failure 11001 {object} data.FailDeleteProject     	"错误返回"
// @Router /api/project/delete [post]
func DeleteProject(c *gin.Context) {
	var param validation.DeleteProject
	if err := c.ShouldBind(&param); err != nil {
		ErrorResponse(c, code.PARAMETER_ERR, err.Error())
		return
	}

	devops := service.NewDevOps()
	err := devops.DeleteProject(param.ID, c.MustGet("curUser").(middleware.User))
	if err != nil {
		ErrorResponse(c, code.DELETE_FAIL, err.Error())
		return
	}
	SuccessResponse(c, "删除成功")
}
